package xyz.zdsoft.user_info_manage_module.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import xyz.zdsoft.common.entity.SysUser;
import xyz.zdsoft.common.service.impl.SysUserServiceImpl;
import xyz.zdsoft.common.utils.Result;
import xyz.zdsoft.user_info_manage_module.mapper.TeacherInfoManagementMapper;
import xyz.zdsoft.user_info_manage_module.pojo.EditTeacherParameter;
import xyz.zdsoft.user_info_manage_module.pojo.InsertTeacherParameter;
import xyz.zdsoft.user_info_manage_module.pojo.TeacherInfo;
import xyz.zdsoft.user_info_manage_module.pojo.TeacherInfoParameter;
import xyz.zdsoft.user_info_manage_module.service.TeacherInfoManagementService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 教师信息管理业务逻辑处理接口实现类
 * @author 以风
 */
@Service
public class TeacherInfoManagementServiceImpl implements TeacherInfoManagementService {
    @Resource
    private TeacherInfoManagementMapper teacherInfoManagementMapper;

    @Resource
    private SysUserServiceImpl sysUserService;


    /**
     * @param page                 分页信息参数
     * @param teacherInfoParameter 教师信息参数
     * @return 教师信息分页列表
     */
    @Override
    public IPage<TeacherInfo> selectPageVo(IPage<TeacherInfo> page, TeacherInfoParameter teacherInfoParameter) {
        IPage<TeacherInfo> teacherInfoIPage = teacherInfoManagementMapper.selectPageVo(page,teacherInfoParameter);
        return teacherInfoIPage;
    }

    /**
     * 新增教师
     *
     * @param insertTeacherParameter 新增教师信息
     * @return 新增教师结果：0：失败，1：成功
     */
    public Result insertTeacher(InsertTeacherParameter insertTeacherParameter) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_no",insertTeacherParameter.getUserNo())
                .eq("role_type",'2');
        int existing =  sysUserService.count(wrapper);
        //如果工号已存在
        if (existing > 0){
            return Result.error().message("该工号已存在，新增教师失败，").data(0);
        }else {
            //新增教师
            SysUser sysUser = new SysUser();
            //姓名
            sysUser.setName(insertTeacherParameter.getName());
            //工号
            sysUser.setUserNo(insertTeacherParameter.getUserNo());
            //身份证号
            sysUser.setIdNo(insertTeacherParameter.getIdNo());
            //性别
            sysUser.setSex(insertTeacherParameter.getSex());
            //手机号
            sysUser.setMobileNo(insertTeacherParameter.getMobileNo());
            //职位
            sysUser.setPosition(insertTeacherParameter.getPosition());

            //TODO 创建者

            //密码
            //截取身份证后六位
            String password = insertTeacherParameter.getIdNo().substring(12,18);
            sysUser.setPassword(password);

            //角色
            sysUser.setRoleType("2");

            boolean flag = sysUserService.save(sysUser);
            if (flag){
                return Result.ok().message("新增教师成功").data(1);
            }else {
                return Result.error().message("新增教师失败").data(0);
            }
        }
    }

    /**
     * 编辑教师信息
     * @param editTeacherParameter 教师信息入参
     * @return 编辑教师信息结果：0：失败，1：成功
     */
    public Result editTeacher(EditTeacherParameter editTeacherParameter) {
        UpdateWrapper<SysUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_no",editTeacherParameter.getUserNo())
                .eq("role_type",'2');
        int existing =  sysUserService.count(wrapper);
        //如果工号已存在
        if (existing > 0){
            return Result.error().message("工号已存在，修改教师信息失败，").data(0);
        }else {
            //修改教师信息
            SysUser sysUser = new SysUser();
            //主键
            sysUser.setId(editTeacherParameter.getId());
            //姓名
            sysUser.setName(editTeacherParameter.getName());
            //工号
            sysUser.setUserNo(editTeacherParameter.getUserNo());
            //身份证号
            sysUser.setIdNo(editTeacherParameter.getIdNo());
            //性别
            sysUser.setSex(editTeacherParameter.getSex());
            //手机号
            sysUser.setMobileNo(editTeacherParameter.getMobileNo());
            //职位
            sysUser.setPosition(editTeacherParameter.getPosition());

            //TODO 修改者

            //角色
            sysUser.setRoleType("2");

            boolean flag = sysUserService.updateById(sysUser);
            if (flag){
                return Result.ok().message("修改教师信息成功").data(1);
            }else {
                return Result.error().message("修改教师信息失败").data(0);
            }
        }
    }


    public Result disableOrEnable(Long id,String delFlag) {
        //启用或停用教师
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setDelFlag(delFlag);

        boolean flag = sysUserService.updateById(sysUser);
        if (flag){
            return Result.ok().message("修改教师信息成功").data(1);
        }else {
            return Result.error().message("修改教师信息失败").data(0);
        }
    }
}
